/*-------------------------------------------------------------------------
   Copyright (C) 2010 One Laptop per Child

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   In other words, you are welcome to use, share and improve this program.
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!

   As a special exception, you may use this file as part of a free software
   library for the XO of the One Laptop per Child project without restriction.
   Specifically, if other files instantiate
   templates or use macros or inline functions from this file, or you compile
   this file and link it with other files to produce an executable, this
   file does not by itself cause the resulting executable to be covered by
   the GNU General Public License.  This exception does not however
   invalidate any other reasons why the executable file might be covered by
   the GNU General Public License.
-------------------------------------------------------------------------*/

#ifndef ONE_WIRE_H
#define ONE_WIRE_H

void intsrv_timer1(void) EXTERN_INTERRUPT(3);

void onewire_init_module(void);
void onewire_enable(void);
void onewire_disable(void);
void onewire_init(void);
void onewire_init_bus(void);
void onewire_process(void);
void SetAbnormalCauseCode( unsigned char nCode );
void OneWireSendByte(unsigned char Byte);
void OneWireRecByte(void);
void onewire_debug_dump(void);
void onewire_enable_show_states(void);

unsigned char onewire_operation_pending(void);
unsigned char onewire_get_battery_type(void);


void onewire_cmd_reset(void);
void onewire_cmd_read_type(void);
void onewire_cmd_read_voltage(void);
void onewire_cmd_read_current(void);
void onewire_cmd_read_acr(void);
void onewire_cmd_read_temperature(void);
void onewire_cmd_read_status(void);
void onewire_cmd_read_soc(void);
void onewire_cmd_read_gauge_id(void);
void onewire_cmd_read_gauge_data(void);
void onewire_cmd_read_error_code(void);
void onewire_cmd_read_battery_minW(void);
void onewire_cmd_read_battery_maxW(void);
void onewire_cmd_reset_battery_minmaxW(void);
void onewire_cmd_read_host_suspend_pwr(void);

extern unsigned char xdata GAUGE_CACHE[144+8];

extern unsigned char xdata bHDQ_ACCESS_FLAG;
#define     	BATT_ACR_UPDATE      (1<<0)
#define     	BATT_EEPROM0_UPDATE  (1<<1)
#define     	BATT_EEPROM1_UPDATE  (1<<2)
#define     	BATT_EEPROM2_UPDATE  (1<<3)
#define     	BATT_EEPROM0_READ    (1<<4)
#define     	BATT_EEPROM1_READ    (1<<5)
#define     	BATT_EEPROM2_READ    (1<<6)
#define     	BATT_DCR_UPDATE      (1<<7)

extern unsigned char xdata bHDQ_ACCESS_FLAG1;
#define     	BATT_REAL_ACR_UPDATE (1<<0)
#define     	BATT_SRAM0_CHECK     (1<<1)
#define     	BATT_SRAM1_CHECK     (1<<2)
#define     	BATT_SRAM2_CHECK     (1<<3)
#define     	BATT_ACR_BACKUP      (1<<4)
#define     	BATT_LAST_ACR_UPDATE (1<<5)
#define     	BATT_ACR_CHECK       (1<<6)
#define     	BATT_EEPROM_RECALL   (1<<7)

extern unsigned char xdata SOC;
extern unsigned char xdata MovingAvgCtr;
extern unsigned int  xdata wBAT_VOLTAGE;
extern signed  int   xdata wBAT_CURRENT;
extern signed  int   xdata wBAT_TEMPERATURE;
extern unsigned char xdata NewACR_BackUp_MSB;
extern unsigned char xdata NewACR_BackUp_LSB;

extern long xdata Ib_inst_in_mA;
extern long xdata Vb_inst_in_mV;
extern int  xdata Pb_inst_in_mW;

#define BATT_ERR_1W_STUCK_LOW		0x01
#define BATT_ERR_PACK_INFO			0x02
// unused #define BATT_ERR_NIMH_TEMP		0x03
// unused #define BATT_ERR_NIMH_OVER_V		0x04
// unused #define BATT_ERR_NIMH_OVER_I		0x05
// unused #define BATT_ERR_NIMH_NO_VCHANGE	0x06
// unused #define BATT_ERR_NIMH_PRECHARGE	0x08

// unused #define BATT_ERR_NO_ID			0x09
#define BATT_ERR_LIFE_OVER_V		0x0A
// unused #define BATT_ERR_LIFE_TEMP		0x0B
#define BATT_ERR_LIFE_NO_VCHANGE	0x0C
// unused #define BATT_ERR_ACR				0x10
// unused #define BATT_ERR_NIMH_FF_CNT		0x11
// unused #define BATT_ERR_FF_CNT			0x12
#define BATT_ERR_VOLT_INVALID		0x13
#define BATT_ERR_BANK0				0x14
#define BATT_ERR_BANK1				0x15
#define BATT_ERR_BANK2				0x16

#define GET_REAL_ACR		((((int)GAUGE_CACHE[DS_ACR_MSB])<<8) | GAUGE_CACHE[DS_ACR_LSB])
#define GET_LAST_DIST_ACR	((((int)GAUGE_CACHE[DS_LAST_DISC_ACR_MSB])<<8) | GAUGE_CACHE[DS_LAST_DISC_ACR_LSB])

#endif //ONE_WIRE_H
